package com.rongshu.view.home.presenter;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.api.RetorfitService;
import com.rongshu.utils.Common;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.mvp.BasePresenter;
import com.rongshu.view.base.mvp.INetBean;
import com.rongshu.view.home.bean.VersionUpdateResponse;
import com.rongshu.view.home.contract.LoginExpView;
import com.rongshu.view.userInfo.entity.MessageInfo;
import com.widget.popupwindow.ArticleStatusPopupWindow;

import java.util.HashMap;
import java.util.Map;

import rx.Subscriber;

public class LoginExpPresenter extends BasePresenter<LoginExpView> {

    public LoginExpPresenter(Context context) {
        super(context);
    }

    /**
     * 每日首次登陆APP获取经验
     */
    public void getFirstLogInExp(int user_id, String sign) {
        RetorfitService.getFirstLogInExp(user_id, sign)
                .subscribe(new Subscriber<INetBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(INetBean bean) {
                        if (getView() != null) {
                            getView().onFirstLoginExp(bean);
                        }
                    }
                });
    }

    /**
     * 获取我的消息页面的未读信息数
     */
    public void getMsgCount(int user_id, String sign) {
        RetorfitService.getMsgCount(user_id, sign)
                .subscribe(new Subscriber<MessageInfo>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(MessageInfo info) {
                        if (getView() != null) {
                            getView().onMsgCount(info);
                        }
                    }
                });
    }

    /**
     * app版本更新
     */
    public void getVersionUpdate(int type, String version, String sign) {
        RetorfitService.checkUpgrade(type, version, sign)
                .compose(getView().<VersionUpdateResponse>bindToLife())
                .subscribe(new Subscriber<VersionUpdateResponse>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(VersionUpdateResponse response) {
                        if (getView() != null) {
                            getView().onVersionUpdate(response);
                        }
                    }
                });
    }

    public void selectTypeFace(FrameLayout container) {
        final ArticleStatusPopupWindow popupWindow = new ArticleStatusPopupWindow(mContext, R.layout.dialog_type_face, 185);
        popupWindow.showAtLocation(container, Gravity.CENTER, 0, 0);
        View contentView = popupWindow.getContentView();
        TextView tab1 = contentView.findViewById(R.id.tab1);
        TextView tab2 = contentView.findViewById(R.id.tab2);
        TextView tab3 = contentView.findViewById(R.id.tab3);
        TextView tab4 = contentView.findViewById(R.id.tab4);
        //type click
        tab1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        tab2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        tab3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();

            }
        });
        tab4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();

            }
        });
    }

    public void GetUserQuiesceLogin() {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        map.put("registrationid", UserInfoUtils.getRegistrationId());
        RetorfitService.getUserQuiesceLogin(UserInfoUtils.getUserId(),
                UserInfoUtils.getRegistrationId(), Common.encryptionSign(map))
                .compose(getView().<INetBean>bindToLife())
                .subscribe(new Subscriber<INetBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(INetBean info) {
                        if (info.getErrCode() != 0) {
                            UserInfoUtils.removeUserInfo();
                        }
                    }
                });
    }
}
